<template>
  <a-modal
    title="查看销售订单"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
            <a-form-item v-show="true" label="订单编号" :labelCol="labelCol_T" :wrapperCol="wrapperCol_T" has-feedback>
              <a-input placeholder="订单编号无需输入" v-decorator="['code']" :disabled="true" />
            </a-form-item>
          </a-col>
        </a-row>
        <saleorder-table-form-item
          :dataSource="workOrderList"
          :proList="list.proList"
          :plaNum="form.getFieldValue('plaNum') ? form.getFieldValue('plaNum') : null"
          :disabled="true"
          @add="
            ({ tableData }) => {
              workOrderList = tableData
            }
          "
          @del="
            ({ tableData }) => {
              workOrderList = tableData
            }
          "
        />
        <a-form-item label="备注信息" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-textarea :rows="3" placeholder="请输入备注信息" v-decorator="['remarks']" :disabled="true" />
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
    </template>
  </a-modal>
</template>
<script>
import { saleOrderEdit } from '@/api/modular/main/saleorder/saleOrderManage'
import { proList } from '@/api/modular/main/pro/proManage'
import saleorderTableFormItem from '@/views/main/saleorder/saleorderTableFormItem'
import XEUtils from 'xe-utils'
export default {
  components: {
    saleorderTableFormItem
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      labelCol_T: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol_T: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      list: {
        // 产品
        proList: []
      },
      // 销售订单
      workOrderList: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  provide() {
    return {
      getProList: this.getProList
    }
  },
  methods: {
    async open(record) {
      this.visible = true
      this.workOrderList = record.workOrderList
      await this.getProList()
      setTimeout(() => {
        this.form.setFieldsValue({
          // 表单接收数据
          code: record.code,
          id: record.id,
          remarks: record.remarks
        })
      }, 100)
    },
    // 获取proList表单
    async getProList() {
      this.list.proList = []
      var promise = proList().then((res) => {
        this.list.proList = res.data
      })
      await promise
      return this.list.proList
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values.code = XEUtils.trim(values.code)
          for (const key in values) {
            if (typeof values[key] === 'object' && values[key] != null) {
              values[key] = JSON.stringify(values[key])
            }
          }
          values.time = this.timeDateString || null
          values.workOrderList = XEUtils.orderBy(this.workOrderList, [['plaNum', 'asc']]) || null
          saleOrderEdit(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message) //  + res.message
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
      this.workOrderList = []
    }
  }
}
</script>
